package com.yupi.shortchain.controller;


import cn.hutool.core.text.CharSequenceUtil;
import com.yupi.shortchain.common.ErrorCode;
import com.yupi.shortchain.exception.BusinessException;
import com.yupi.shortchain.model.entity.UrlRelate;
import com.yupi.shortchain.service.UrlRelateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.annotation.Resource;

/**
 * 短链重定向
 */
@Controller
@RequestMapping("/redirect")
public class ShortLinkRedirectController {

    @Resource
    private UrlRelateService urlRelateService;

    public ModelAndView redirectToLongUrl(String shortUrl,String password){
        // 此处需要实现逻辑将长链接映射到短链接
        // 可以根据你的需求将长链接与短链接关联起来
        UrlRelate urlRelate = urlRelateService.getLongLink(shortUrl);
        if (CharSequenceUtil.isNotBlank(urlRelate.getPassword()) && !urlRelate.getPassword().equals(password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误");
        }
        return new ModelAndView(new RedirectView(urlRelate.getLongUrl(), false));

    }
}
